﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.Data.Day
{
    /// <summary>
    /// 账户日持仓汇总
    /// </summary>
    public class AccountDayPositionDelivery
    {
        public int Id { get; set; }

        public int AccountId { get; set; }

        public virtual Sys.Account Account { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 合约
        /// </summary>
        public string InstrumentId { get; set; }
        /// <summary>
        /// 经纪公司代码
        /// </summary>
        public string Brokerid { get; set; }
        /// <summary>
        /// 投资者代码
        /// </summary>
        public string Investorid { get; set; }
        /// <summary>
        /// 投保
        /// </summary>
        public string HedgeFlag { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public string Direction { get; set; }
        /// <summary>
        /// 开仓时间
        /// </summary>
        public int OpenTime { get; set; }
        /// <summary>
        /// 开仓日期
        /// </summary>
        public int OpenDate { get; set; }
        /// <summary>
        /// 成交编号
        /// </summary>
        public string TradeId { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public int Volume { get; set; }
        /// <summary>
        /// 开仓价
        /// </summary>
        public double OpenPrice { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public string TradingDay { get; set; }
        /// <summary>
        /// 结算编号
        /// </summary>
        public int SettleMentId { get; set; }
        /// <summary>
        /// 成交类型
        /// </summary>
        public string TradeType { get; set; }
        /// <summary>
        /// 组合合约代码
        /// </summary>
        public string CombinStrumentId { get; set; }
        /// <summary>
        /// 交易所id
        /// </summary>
        public string ExchangeId { get; set; }
        /// <summary>
        /// 逐日盯市平仓盈亏
        /// </summary>
        public double CloseProfitByDate { get; set; }
        /// <summary>
        /// 逐笔对冲平仓盈亏
        /// </summary>
        public double CloseProfitByTrade { get; set; }
        /// <summary>
        /// 逐日盯市持仓盈亏
        /// </summary>
        public double PositionProfitByDate { get; set; }
        /// <summary>
        /// 逐笔对冲持仓盈亏
        /// </summary>
        public double PositionProfitByTrade { get; set; }
        /// <summary>
        /// 保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 交易所保证金
        /// </summary>
        public double Exchmargin { get; set; }
        /// <summary>
        /// 保证金率
        /// </summary>
        public double MarginRateByMoney { get; set; }
        /// <summary>
        /// 保证金率(按手数)
        /// </summary>
        public double MarginRateByVolume { get; set; }
        /// <summary>
        /// 昨结算
        /// </summary>
        public double YesterdaySettlement { get; set; }
        /// <summary>
        /// 结算价
        /// </summary>
        public double SettleMentPrice { get; set; }
        /// <summary>
        /// 平仓量
        /// </summary>
        public int CloseVolume { get; set; }
        /// <summary>
        /// 平仓金额
        /// </summary>
        public double CloseAmount { get; set; }
        /// <summary>
        /// 品种
        /// </summary>
        public string ProductId { get; set; }
        /// <summary>
        /// 浮动盈亏
        /// </summary>
        public double FloatingProfit { get; set; }
        /// <summary>
        /// 盯市盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }


        public void InitAccountDayPositionDelivery(int tradeHistoryId, string pdate, DB.DBManager db)
        {
            List<Data.ServerData.T_Account> serverAccounts = new List<ServerData.T_Account>();
            int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
            for (int i = 0; i < connectionStringCount; i++)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                {
                    Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                    List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetList();
                    foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                    {
                        serverAccounts.Add(_account);
                    }
                }
            }
            foreach (Data.ServerData.T_Account account in serverAccounts)
            {
                Sys.Account _account = new Sys.Account();
                int acountid = _account.GetAccountId(account.LoginAccount);
                Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                var query = db.PositionDetailHistory.Where(a => a.AccountId == account.Id).Where(a => a.TradingDay.Equals(pdate));
                foreach (Data.ServerData.T_PositionDetail_History s in query)
                {
                    Data.Day.AccountDayPositionDelivery change = new Data.Day.AccountDayPositionDelivery();
                    change.AccountId = acountid;
                    change.Brokerid = s.Brokerid;
                    change.CloseAmount = s.CloseAmount;
                    change.ExchangeId = s.ExchangeId;
                    change.Exchmargin = s.Exchmargin;
                    change.CloseProfitByDate = s.CloseProfitByDate;
                    change.CloseProfitByTrade = s.CloseProfitByTrade;
                    change.CloseVolume = s.CloseVolume;
                    change.CombinStrumentId = s.CombinStrumentId;
                    change.Commission = s.Commission;
                    change.Direction = s.Direction;
                    change.FloatingProfit = s.FloatingProfit;
                    change.HedgeFlag = s.HedgeFlag;
                    change.InstrumentId = s.InstrumentId;
                    change.Investorid = s.Investorid;
                    change.Margin = s.Margin;
                    change.MarginRateByMoney = s.MarginRateByMoney;
                    change.MarginRateByVolume = s.MarginRateByVolume;
                    change.OpenDate = s.OpenDate;
                    change.OpenPrice = s.OpenPrice;
                    change.OpenTime = s.OpenTime;
                    change.PositionProfit = s.PositionProfit;
                    change.PositionProfitByDate = s.PositionProfitByDate;
                    change.PositionProfitByTrade = s.PositionProfitByTrade;
                    change.ProductId = s.ProductId;
                    change.SettleMentPrice = s.SettleMentPrice;
                    change.TradeHistoryId = tradeHistoryId;
                    change.TradeId = s.TradeId;
                    change.TradeType = s.TradeType;
                    change.TradingDay = s.TradingDay;
                    change.Volume = s.Volume;
                    change.YesterdaySettlement = s.YesterdaySettlement;
                    db.AccountDayPositionDelivery.Add(change);
                }
            }
            db.SaveChanges();
        }

        public List<AccountDayPositionDelivery> GetAccountDayPositionDelivery(int accountid, int tradeHistoryId, DB.DBManager db)
        {
            var query = db.AccountDayPositionDelivery.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.AccountId == accountid).ToList();
            return query;
        }

        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.AccountDayPositionDelivery.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (AccountDayPositionDelivery del in query)
            {
                db.AccountDayPositionDelivery.Remove(del);
            }
            db.SaveChanges();
        }
    }
}
